import {
  queryPageDataReq,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
  queryAllListReq,
  queryPageDataReq2,
  addItemReq2,
  deleteItemReq2,
  updateItemReq2,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";
import {
  queryLabelReq,
  queryVenueReq,
} from "@/pages/stadium/classroom/services";

export default {
  namespace,
  state: {
    init: false,
    data: {},
    allList: [],
    venueArr: [],
    labelArr: [],
  },
  effects: {
    *pageInit({ params }, { call, put }) {
      // const allList = yield call(queryAllListReq, params)
      const venueArr = yield call(queryVenueReq, params);
      const labelArr = yield call(queryLabelReq, params);

      yield put({
        type: "save",
        payload: {
          // allList,
          venueArr: venueArr.records,
          labelArr: labelArr.records,
        },
      });
    },
    *queryTable({ params }, { call, put }) {
      const res = yield call(queryPageDataReq, params);
      yield put({
        type: "save",
        payload: {
          ...params,
          ...res,
        },
      });
      return {
        success: true,
        data: res.records,
        total: res.total,
      };
    },
    *queryTable2({ params }, { call, put }) {
      const res = yield call(queryPageDataReq2, params);
      yield put({
        type: "save",
        payload: {
          ...params,
          ...res,
        },
      });
      return {
        success: true,
        data: res.records,
        total: res.total,
      };
    },

    *submitItem({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq, params);
      } else {
        res = yield call(addItemReq, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },
    *submitItem2({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq2, params);
      } else {
        res = yield call(addItemReq2, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem2({ params, callback }, { call }) {
      let res = yield call(deleteItemReq2, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
